FROM openfaas/of-watchdog:0.5.3 as watchdog
FROM golang:1.13-alpine3.11 as builder

# Allows you to add additional packages via build-arg
ARG ADDITIONAL_PACKAGE
ARG CGO_ENABLED=0
ARG GO111MODULE="on"
ARG GOPROXY="https://mirrors.aliyun.com/goproxy/"
ARG GOFLAGS=""

COPY --from=watchdog /fwatchdog /usr/bin/fwatchdog
RUN chmod +x /usr/bin/fwatchdog

ENV CGO_ENABLED=0

WORKDIR /go/src/handler
COPY . .

WORKDIR /go/src/handler

RUN CGO_ENABLED=${CGO_ENABLED} GOOS=linux \
    go build --ldflags "-s -w" -a -installsuffix cgo -o handler .

FROM alpine:3.11
RUN apk --no-cache add ca-certificates \
    && addgroup -S app && adduser -S -g app app \
    && mkdir -p /home/app \
    && chown app /home/app

WORKDIR /home/app

COPY --from=builder /usr/bin/fwatchdog         .
COPY --from=builder /go/src/handler/function/  .
COPY --from=builder /go/src/handler/handler    .
RUN chmod +x fwatchdog
RUN chown -R app /home/app

USER app

ENV fprocess="./handler"
ENV cgi_headers="true"
ENV mode="http"
ENV upstream_url="http://127.0.0.1:9033"
EXPOSE 8080

HEALTHCHECK --interval=3s CMD [ -e /tmp/.lock ] || exit 1

CMD ["./fwatchdog"]
